home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / Misc. C ƒ / Recorder / DumpRecord.c next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  2.1 KB  |  118 lines  |  [TEXT/MPS ]

  1. /*    File: Recorder.c
  2.  
  3.     MPW Tool for dumping recorder files
  4.     
  5. */
  6.  
  7.  
  8. #include <Types.h>
  9. #include <QuickDraw.h>
  10. #include <Files.h>
  11. #include <Strings.h>
  12.  
  13. #include <StdIO.h>
  14. #include <StdLib.h>
  15.  
  16.  
  17. #define    ONE_LINE    16
  18. short    lineCnt;
  19. short    lastHeader;
  20. char    *headers[] = {    "            ",
  21.                         "Computer -->",
  22.                         "Modem    -->" };
  23.                         
  24. void DumpHexString ( char *theStr, short len ) {
  25.     short    filler;
  26.     char    *hex = "0123456789ABCDEF";
  27.     
  28.     filler = ONE_LINE - len;
  29.     printf ( "$\"" );
  30.     while ( len-- > 0 ) {
  31.         printf ( " %c%c", hex [ ( *theStr >> 4 ) & 0x0F ], hex [ *theStr & 0x0F ] );
  32.         theStr++;
  33.         }
  34.     putchar ( '"' );
  35.     while ( filler-- > 0 )
  36.         printf ( "   " );
  37.     }
  38.  
  39. void DumpAsciiString ( char *theStr, short len ) {
  40.     short    filler;
  41.     
  42.     filler = ONE_LINE - len;
  43.     printf ( "/* " );
  44.     while ( len-- > 0 ) {
  45.         if ( *theStr >= ' ' && *theStr <= '~' )
  46.             putchar ( *theStr );
  47.         else
  48.             putchar ( '.' );
  49.         theStr++;
  50.         }
  51.     
  52.     while ( filler-- > 0 )
  53.         putchar ( ' ' );
  54.     printf ( " */" );
  55.     }
  56.  
  57.  
  58. void DumpLine ( short header, char *theStr, short len ) {
  59.  
  60.     if ( len > 0 ) {
  61.         header -= '0';
  62.         printf ( "%s", headers [ header == lastHeader ? 0 : header ] );
  63.         lastHeader = header;
  64.         
  65.         DumpHexString ( theStr, len );
  66.         putchar ( '\t' );
  67.         putchar ( '\t' );
  68.         DumpAsciiString ( theStr, len );
  69.         printf ( "\n" );
  70.         lineCnt = 0;
  71.         }
  72.     }
  73.  
  74.  
  75. int main ( int argc, char *argv[] ) {
  76.     short    lastDir, dirChar;
  77.     FILE    *inf;
  78.     char    lineBuf    [ ONE_LINE ];
  79.     
  80.     if ( argc != 2 ) {
  81.         fprintf ( stderr, "Usage: %s <fileName>\n", argv [ 0 ] );
  82.         exit ( 1 );
  83.         }
  84.     
  85.     inf = fopen ( argv [ 1 ], "rb" );
  86.     if ( inf == NULL ) {
  87.         fprintf ( stderr, "Unable to open file %s", argv [ 1 ] );
  88.         exit ( 2 );
  89.         }
  90.         
  91.     lastDir = 0;
  92.     lastHeader = -1;
  93.     lineCnt = 0;
  94.     
  95.     do {
  96.     
  97.     /*    End of file, we're done */
  98.         if (( dirChar = fgetc ( inf )) == EOF ) {
  99.             DumpLine ( lastDir, lineBuf, lineCnt );
  100.             break;
  101.             }
  102.         
  103.     /*    Switch data flow */
  104.         if ( dirChar != lastDir ) {
  105.             DumpLine ( lastDir, lineBuf, lineCnt );
  106.             lastDir = dirChar;
  107.             }
  108.         
  109.         lineBuf [ lineCnt++ ] = fgetc ( inf );
  110.         if ( lineCnt == ONE_LINE )
  111.             DumpLine ( lastDir, lineBuf, lineCnt );
  112.  
  113.         } while ( true );
  114.     
  115.     fclose ( inf );
  116.     return 0;
  117.     }
  118.